package com.ll.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 2020/9/14 - 10:33
 * 自定义拦截器
 */
public class MyInterceptor2 implements HandlerInterceptor {
    /**
     * 预处理,Controller方法执行前
     * return true 放行, 执行下一个拦截器,如果没有, 执行controller中的方法
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor2 => preHandle");
//        request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response);
        return true;
    }

    /**
     * 后处理方法, controller执行后  success.jsp 执行前
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response);
        System.out.println("MyInterceptor2 => postHandle");
    }

    /**
     * success.jsp 页面执行后, 该方法执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor2 => afterCompletion");
    }
}
